#include "ibm.h"
#include "plat-keyboard.h"
#include "keyboard.h"

int keybsendcallback = 0;

typedef struct
{
        int scancodes_make[9];
        int scancodes_break[9];        
} scancode;

/*272 = 256 + 16 fake interim scancodes for disambiguation purposes.*/
static scancode scancode_set1[272] =
{
        { {-1},       {-1} },       { {0x01, -1}, {0x81, -1} }, { {0x02, -1}, {0x82, -1} }, { {0x03, -1}, {0x83, -1} },
        { {0x04, -1}, {0x84, -1} }, { {0x05, -1}, {0x85, -1} }, { {0x06, -1}, {0x86, -1} }, { {0x07, -1}, {0x87, -1} },
        { {0x08, -1}, {0x88, -1} }, { {0x09, -1}, {0x89, -1} }, { {0x0a, -1}, {0x8a, -1} }, { {0x0b, -1}, {0x8b, -1} },
        { {0x0c, -1}, {0x8c, -1} }, { {0x0d, -1}, {0x8d, -1} }, { {0x0e, -1}, {0x8e, -1} }, { {0x0f, -1}, {0x8f, -1} },
        { {0x10, -1}, {0x90, -1} }, { {0x11, -1}, {0x91, -1} }, { {0x12, -1}, {0x92, -1} }, { {0x13, -1}, {0x93, -1} },
        { {0x14, -1}, {0x94, -1} }, { {0x15, -1}, {0x95, -1} }, { {0x16, -1}, {0x96, -1} }, { {0x17, -1}, {0x97, -1} },
        { {0x18, -1}, {0x98, -1} }, { {0x19, -1}, {0x99, -1} }, { {0x1a, -1}, {0x9a, -1} }, { {0x1b, -1}, {0x9b, -1} },
        { {0x1c, -1}, {0x9c, -1} }, { {0x1d, -1}, {0x9d, -1} }, { {0x1e, -1}, {0x9e, -1} }, { {0x1f, -1}, {0x9f, -1} },
        { {0x20, -1}, {0xa0, -1} }, { {0x21, -1}, {0xa1, -1} }, { {0x22, -1}, {0xa2, -1} }, { {0x23, -1}, {0xa3, -1} },
        { {0x24, -1}, {0xa4, -1} }, { {0x25, -1}, {0xa5, -1} }, { {0x26, -1}, {0xa6, -1} }, { {0x27, -1}, {0xa7, -1} },
        { {0x28, -1}, {0xa8, -1} }, { {0x29, -1}, {0xa9, -1} }, { {0x2a, -1}, {0xaa, -1} }, { {0x2b, -1}, {0xab, -1} },
        { {0x2c, -1}, {0xac, -1} }, { {0x2d, -1}, {0xad, -1} }, { {0x2e, -1}, {0xae, -1} }, { {0x2f, -1}, {0xaf, -1} },
        { {0x30, -1}, {0xb0, -1} }, { {0x31, -1}, {0xb1, -1} }, { {0x32, -1}, {0xb2, -1} }, { {0x33, -1}, {0xb3, -1} },
        { {0x34, -1}, {0xb4, -1} }, { {0x35, -1}, {0xb5, -1} }, { {0x36, -1}, {0xb6, -1} }, { {0x37, -1}, {0xb7, -1} },
        { {0x38, -1}, {0xb8, -1} }, { {0x39, -1}, {0xb9, -1} }, { {0x3a, -1}, {0xba, -1} }, { {0x3b, -1}, {0xbb, -1} },
        { {0x3c, -1}, {0xbc, -1} }, { {0x3d, -1}, {0xbd, -1} }, { {0x3e, -1}, {0xbe, -1} }, { {0x3f, -1}, {0xbf, -1} },
        { {0x40, -1}, {0xc0, -1} }, { {0x41, -1}, {0xc1, -1} }, { {0x42, -1}, {0xc2, -1} }, { {0x43, -1}, {0xc3, -1} },
        { {0x44, -1}, {0xc4, -1} }, { {0x45, -1}, {0xc5, -1} }, { {0x46, -1}, {0xc6, -1} }, { {0x47, -1}, {0xc7, -1} },
        { {0x48, -1}, {0xc8, -1} }, { {0x49, -1}, {0xc9, -1} }, { {0x4a, -1}, {0xca, -1} }, { {0x4b, -1}, {0xcb, -1} },
        { {0x4c, -1}, {0xcc, -1} }, { {0x4d, -1}, {0xcd, -1} }, { {0x4e, -1}, {0xce, -1} }, { {0x4f, -1}, {0xcf, -1} },
        { {0x50, -1}, {0xd0, -1} }, { {0x51, -1}, {0xd1, -1} }, { {0x52, -1}, {0xd2, -1} }, { {0x53, -1}, {0xd3, -1} },
        { {0x54, -1}, {0xd4, -1} }, { {0x55, -1}, {0xd5, -1} }, { {0x56, -1}, {0xd6, -1} }, { {0x57, -1}, {0xd7, -1} },
        { {0x58, -1}, {0xd8, -1} }, { {0x59, -1}, {0xd9, -1} }, { {0x5a, -1}, {0xda, -1} }, { {0x5b, -1}, {0xdb, -1} },
        { {0x5c, -1}, {0xdc, -1} }, { {0x5d, -1}, {0xdd, -1} }, { {0x5e, -1}, {0xde, -1} }, { {0x5f, -1}, {0xdf, -1} },
        { {0x60, -1}, {0xe0, -1} }, { {0x61, -1}, {0xe1, -1} }, { {0x62, -1}, {0xe2, -1} }, { {0x63, -1}, {0xe3, -1} },
        { {0x64, -1}, {0xe4, -1} }, { {0x65, -1}, {0xe5, -1} }, { {0x66, -1}, {0xe6, -1} }, { {0x67, -1}, {0xe7, -1} },
        { {0x68, -1}, {0xe8, -1} }, { {0x69, -1}, {0xe9, -1} }, { {0x6a, -1}, {0xea, -1} }, { {0x6b, -1}, {0xeb, -1} },
        { {0x6c, -1}, {0xec, -1} }, { {0x6d, -1}, {0xed, -1} }, { {0x6e, -1}, {0xee, -1} }, { {0x6f, -1}, {0xef, -1} },
        { {0x70, -1}, {0xf0, -1} }, { {0x71, -1}, {0xf1, -1} }, { {0x72, -1}, {0xf2, -1} }, { {0x73, -1}, {0xf3, -1} },
        { {0x74, -1}, {0xf4, -1} }, { {0x75, -1}, {0xf5, -1} }, { {0x76, -1}, {0xf6, -1} }, { {0x77, -1}, {0xf7, -1} },
        { {0x78, -1}, {0xf8, -1} }, { {0x79, -1}, {0xf9, -1} }, { {0x7a, -1}, {0xfa, -1} }, { {0x7b, -1}, {0xfb, -1} },
        { {0x7c, -1}, {0xfc, -1} }, { {0x7d, -1}, {0xfd, -1} }, { {0x7e, -1}, {0xfe, -1} }, { {0x7f, -1}, {0xff, -1} },

        { {0x80, -1},       {-1} },             { {0x81, -1},       {-1} },             { {0x82, -1},       {-1} },             { {0xe0, 0x03, -1}, {0xe0, 0x83, -1} }, /*80*/
        { {0xe0, 0x04, -1}, {0xe0, 0x84, -1} }, { {0x85, -1},       {-1} },             { {0x86, -1},       {-1} },             { {0x87, -1},       {-1} },             /*84*/
        { {0xe0, 0x08, -1}, {0xe0, 0x88, -1} }, { {0xe0, 0x09, -1}, {0xe0, 0x89, -1} }, { {0xe0, 0x0a, -1}, {0xe0, 0x8a, -1} }, { {0xe0, 0x0b, -1}, {0xe0, 0x8b, -1} }, /*88*/
        { {0xe0, 0x0c, -1}, {0xe0, 0x8c, -1} }, { {-1},             {-1} },             { {0xe0, 0x0e, -1}, {0xe0, 0x8e, -1} }, { {0xe0, 0x0f, -1}, {0xe0, 0x8f, -1} }, /*8c*/
        { {0xe0, 0x10, -1}, {0xe0, 0x90, -1} }, { {0xe0, 0x11, -1}, {0xe0, 0x91, -1} }, { {0xe0, 0x12, -1}, {0xe0, 0x92, -1} }, { {0xe0, 0x13, -1}, {0xe0, 0x93, -1} }, /*90*/
        { {0xe0, 0x14, -1}, {0xe0, 0x94, -1} }, { {0xe0, 0x15, -1}, {0xe0, 0x95, -1} }, { {0xe0, 0x16, -1}, {0xe0, 0x96, -1} }, { {0xe0, 0x17, -1}, {0xe0, 0x97, -1} }, /*94*/
        { {0xe0, 0x18, -1}, {0xe0, 0x98, -1} }, { {0xe0, 0x19, -1}, {0xe0, 0x99, -1} }, { {0xe0, 0x1a, -1}, {0xe0, 0x9a, -1} }, { {0xe0, 0x1b, -1}, {0xe0, 0x9b, -1} }, /*98*/
        { {0xe0, 0x1c, -1}, {0xe0, 0x9c, -1} }, { {0xe0, 0x1d, -1}, {0xe0, 0x9d, -1} }, { {0xe0, 0x1e, -1}, {0xe0, 0x9e, -1} }, { {0xe0, 0x1f, -1}, {0xe0, 0x9f, -1} }, /*9c*/
        { {0xe0, 0x20, -1}, {0xe0, 0xa0, -1} }, { {0xe0, 0x21, -1}, {0xe0, 0xa1, -1} }, { {0xe0, 0x22, -1}, {0xe0, 0xa2, -1} }, { {0xe0, 0x23, -1}, {0xe0, 0xa3, -1} }, /*a0*/
        { {0xe0, 0x24, -1}, {0xe0, 0xa4, -1} }, { {0xe0, 0x25, -1}, {0xe0, 0xa5, -1} }, { {0xe0, 0x26, -1}, {0xe0, 0xa6, -1} }, { {-1},             {-1} },             /*a4*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*a8*/
        { {0xe0, 0x2c, -1}, {0xe0, 0xac, -1} }, { {0xe0, 0x2d, -1}, {0xe0, 0xad, -1} }, { {0xe0, 0x2e, -1}, {0xe0, 0xae, -1} }, { {0xe0, 0x2f, -1}, {0xe0, 0xaf, -1} }, /*ac*/
        { {0xe0, 0x30, -1}, {0xe0, 0xb0, -1} }, { {0xe0, 0x31, -1}, {0xe0, 0xb1, -1} }, { {0xe0, 0x32, -1}, {0xe0, 0xb2, -1} }, { {-1},             {-1} },             /*b0*/
        { {0xe0, 0x34, -1}, {0xe0, 0xb4, -1} }, { {0xe0, 0x35, -1}, {0xe0, 0xb5, -1} }, { {-1},             {-1} },             { {0xe0, 0x37, -1}, {0xe0, 0xb7, -1} }, /*b4*/
        { {0xe0, 0x38, -1}, {0xe0, 0xb8, -1} }, { {-1},             {-1} },             { {0xe0, 0x3a, -1}, {0xe0, 0xba, -1} }, { {0xe0, 0x3b, -1}, {0xe0, 0xbb, -1} }, /*b8*/
        { {0xe0, 0x3c, -1}, {0xe0, 0xbc, -1} }, { {0xe0, 0x3d, -1}, {0xe0, 0xbd, -1} }, { {0xe0, 0x3e, -1}, {0xe0, 0xbe, -1} }, { {0xe0, 0x3f, -1}, {0xe0, 0xbf, -1} }, /*bc*/
        { {0xe0, 0x40, -1}, {0xe0, 0xc0, -1} }, { {0xe0, 0x41, -1}, {0xe0, 0xc1, -1} }, { {0xe0, 0x42, -1}, {0xe0, 0xc2, -1} }, { {0xe0, 0x43, -1}, {0xe0, 0xc3, -1} }, /*c0*/
        { {0xe0, 0x44, -1}, {0xe0, 0xc4, -1} }, { {-1},             {-1} },             { {0xe0, 0x46, -1}, {0xe0, 0xc6, -1} }, { {0xe0, 0xaa, 0xe0, 0x47, -1}, {0xe0, 0xc7, 0xe0, 0x2a, -1} }, /*c4*/
        { {0xe0, 0xaa, 0xe0, 0x48, -1}, {0xe0, 0xc8, 0xe0, 0x2a, -1} }, { {0xe0, 0xaa, 0xe0, 0x49, -1}, {0xe0, 0xc9, 0xe0, 0x2a, -1} }, { {-1},             {-1} },             { {0xe, 0xaa, 0xe0, 0x4b, -1}, {0xe0, 0xcb, 0xe0, 0x2a, -1} }, /*c8*/
        { {0xe0, 0x4c, -1}, {0xe0, 0xcc, -1} }, { {0xe0, 0xaa, 0xe0, 0x4d, -1}, {0xe0, 0xcd, 0xe0, 0x2a, -1} }, { {0xe0, 0x4e, -1}, {0xe0, 0xce, -1} }, { {0xe0, 0xaa, 0xe0, 0x4f, -1}, {0xe0, 0xcf, 0xe0, 0x2a, -1} }, /*cc*/
        { {0xe0, 0xaa, 0xe0, 0x50, -1}, {0xe0, 0xd0, 0xe0, 0x2a, -1} }, { {0xe0, 0xaa, 0xe0, 0x51, -1}, {0xe0, 0xd1, 0xe0, 0x2a, -1} }, { {0xe0, 0xaa, 0xe0, 0x52, -1}, {0xe0, 0xd2, 0xe0, 0x2a, -1} }, { {0xe0, 0xaa, 0xe0, 0x53, -1}, {0xe0, 0xd3, 0xe0, 0x2a, -1} }, /*d0*/
        { {0xd4, -1},       {-1} },             { {0xe0, 0x55, -1}, {0xe0, 0xd5, -1} }, { {-1},             {-1} },             { {0xe0, 0x57, -1}, {0xe0, 0xd7, -1} }, /*d4*/
        { {0xe0, 0x58, -1}, {0xe0, 0xd8, -1} }, { {0xe0, 0x59, -1}, {0xe0, 0xd9, -1} }, { {0xe0, 0x5a, -1}, {0xe0, 0xaa, -1} }, { {0xe0, 0x5b, -1}, {0xe0, 0xdb, -1} }, /*d8*/
        { {0xe0, 0x5c, -1}, {0xe0, 0xdc, -1} }, { {0xe0, 0x5d, -1}, {0xe0, 0xdd, -1} }, { {0xe0, 0x5e, -1}, {0xe0, 0xee, -1} }, { {0xe0, 0x5f, -1}, {0xe0, 0xdf, -1} }, /*dc*/
        { {-1},             {-1} },             { {0xe0, 0x61, -1}, {0xe0, 0xe1, -1} }, { {0xe0, 0x62, -1}, {0xe0, 0xe2, -1} }, { {0xe0, 0x63, -1}, {0xe0, 0xe3, -1} }, /*e0*/
        { {0xe0, 0x64, -1}, {0xe0, 0xe4, -1} }, { {0xe0, 0x65, -1}, {0xe0, 0xe5, -1} }, { {0xe0, 0x66, -1}, {0xe0, 0xe6, -1} }, { {0xe0, 0x67, -1}, {0xe0, 0xe7, -1} }, /*e4*/
        { {0xe0, 0x68, -1}, {0xe0, 0xe8, -1} }, { {0xe0, 0x69, -1}, {0xe0, 0xe9, -1} }, { {0xe0, 0x6a, -1}, {0xe0, 0xea, -1} }, { {0xe0, 0x6b, -1}, {0xe0, 0xeb, -1} }, /*e8*/
        { {0xe0, 0x6c, -1}, {0xe0, 0xec, -1} }, { {0xe0, 0x6d, -1}, {0xe0, 0xed, -1} }, { {0xe0, 0x6e, -1}, {0xe0, 0xee, -1} }, { {-1},             {-1} },             /*ec*/
        { {0xe0, 0x70, -1}, {0xe0, 0xf0, -1} }, { {0xf1, -1},       {-1} },             { {0xf2, -1},       {-1} },             { {0xe0, 0x73, -1}, {0xe0, 0xf3, -1} }, /*f0*/
        { {0xe0, 0x74, -1}, {0xe0, 0xf4, -1} }, { {0xe0, 0x75, -1}, {0xe0, 0xf5, -1} }, { {-1},             {-1} },             { {0xe0, 0x77, -1}, {0xe0, 0xf7, -1} }, /*f4*/
        { {0xe0, 0x78, -1}, {0xe0, 0xf8, -1} }, { {0xe0, 0x79, -1}, {0xe0, 0xf9, -1} }, { {0xe0, 0x7a, -1}, {0xe0, 0xfa, -1} }, { {0xe0, 0x7b, -1}, {0xe0, 0xfb, -1} }, /*f8*/
        { {0xe0, 0x7c, -1}, {0xe0, 0xfc, -1} }, { {0xe0, 0x7d, -1}, {0xe0, 0xfd, -1} }, { {0xe0, 0x7e, -1}, {0xe0, 0xfe, -1} }, { {0xe1, 0x1d, -1}, {0xe1, 0x9d, -1} }, /*fc*/
        
	{ {-1},             {-1} },             { {0xe0, 0x01, -1}, {0xe0, 0x81, -1} }, { {0xe0, 0x02, -1}, {0xe0, 0x82, -1} }, { {0xe0, 0xaa, -1}, {0xe0, 0x2a, -1} }, /*100*/
        { {-1},             {-1} },             { {0xe0, 0x05, -1}, {0xe0, 0x85, -1} }, { {0xe0, 0x06, -1}, {0xe0, 0x86, -1} }, { {0xe0, 0x07, -1}, {0xe0, 0x87, -1} }, /*104*/
        { {0xe0, 0x71, -1}, {0xe0, 0xf1, -1} }, { {0xe0, 0x72, -1}, {0xe0, 0xf2, -1} }, { {0xe0, 0x7f, -1}, {0xe0, 0xff, -1} }, { {0xe0, 0xe1, -1}, {-1} },             /*108*/
        { {0xe0, 0xee, -1}, {-1} },             { {0xe0, 0xf1, -1}, {-1} },             { {0xe0, 0xfe, -1}, {-1} },             { {0xe0, 0xff, -1}, {-1} }              /*10c*/
};

static scancode scancode_set2[272] =
{
        { {-1},       {-1} },             { {0x76, -1}, {0xF0, 0x76, -1} }, { {0x16, -1}, {0xF0, 0x16, -1} }, { {0x1E, -1}, {0xF0, 0x1E, -1} },
        { {0x26, -1}, {0xF0, 0x26, -1} }, { {0x25, -1}, {0xF0, 0x25, -1} }, { {0x2E, -1}, {0xF0, 0x2E, -1} }, { {0x36, -1}, {0xF0, 0x36, -1} },
        { {0x3D, -1}, {0xF0, 0x3D, -1} }, { {0x3E, -1}, {0xF0, 0x3E, -1} }, { {0x46, -1}, {0xF0, 0x46, -1} }, { {0x45, -1}, {0xF0, 0x45, -1} },
        { {0x4E, -1}, {0xF0, 0x4E, -1} }, { {0x55, -1}, {0xF0, 0x55, -1} }, { {0x66, -1}, {0xF0, 0x66, -1} }, { {0x0D, -1}, {0xF0, 0x0D, -1} },
        { {0x15, -1}, {0xF0, 0x15, -1} }, { {0x1D, -1}, {0xF0, 0x1D, -1} }, { {0x24, -1}, {0xF0, 0x24, -1} }, { {0x2D, -1}, {0xF0, 0x2D, -1} },
        { {0x2C, -1}, {0xF0, 0x2C, -1} }, { {0x35, -1}, {0xF0, 0x35, -1} }, { {0x3C, -1}, {0xF0, 0x3C, -1} }, { {0x43, -1}, {0xF0, 0x43, -1} },
        { {0x44, -1}, {0xF0, 0x44, -1} }, { {0x4D, -1}, {0xF0, 0x4D, -1} }, { {0x54, -1}, {0xF0, 0x54, -1} }, { {0x5B, -1}, {0xF0, 0x5B, -1} },
        { {0x5A, -1}, {0xF0, 0x5A, -1} }, { {0x14, -1}, {0xF0, 0x14, -1} }, { {0x1C, -1}, {0xF0, 0x1C, -1} }, { {0x1B, -1}, {0xF0, 0x1B, -1} },
        { {0x23, -1}, {0xF0, 0x23, -1} }, { {0x2B, -1}, {0xF0, 0x2B, -1} }, { {0x34, -1}, {0xF0, 0x34, -1} }, { {0x33, -1}, {0xF0, 0x33, -1} },
        { {0x3B, -1}, {0xF0, 0x3B, -1} }, { {0x42, -1}, {0xF0, 0x42, -1} }, { {0x4B, -1}, {0xF0, 0x4B, -1} }, { {0x4C, -1}, {0xF0, 0x4C, -1} },
        { {0x52, -1}, {0xF0, 0x52, -1} }, { {0x0E, -1}, {0xF0, 0x0E, -1} }, { {0x12, -1}, {0xF0, 0x12, -1} }, { {0x5D, -1}, {0xF0, 0x5D, -1} },
        { {0x1A, -1}, {0xF0, 0x1A, -1} }, { {0x22, -1}, {0xF0, 0x22, -1} }, { {0x21, -1}, {0xF0, 0x21, -1} }, { {0x2A, -1}, {0xF0, 0x2A, -1} },
        { {0x32, -1}, {0xF0, 0x32, -1} }, { {0x31, -1}, {0xF0, 0x31, -1} }, { {0x3A, -1}, {0xF0, 0x3A, -1} }, { {0x41, -1}, {0xF0, 0x41, -1} },
        { {0x49, -1}, {0xF0, 0x49, -1} }, { {0x4A, -1}, {0xF0, 0x4A, -1} }, { {0x59, -1}, {0xF0, 0x59, -1} }, { {0x7C, -1}, {0xF0, 0x7C, -1} },
        { {0x11, -1}, {0xF0, 0x11, -1} }, { {0x29, -1}, {0xF0, 0x29, -1} }, { {0x58, -1}, {0xF0, 0x58, -1} }, { {0x05, -1}, {0xF0, 0x05, -1} },
        { {0x06, -1}, {0xF0, 0x06, -1} }, { {0x04, -1}, {0xF0, 0x04, -1} }, { {0x0C, -1}, {0xF0, 0x0C, -1} }, { {0x03, -1}, {0xF0, 0x03, -1} },
        { {0x0B, -1}, {0xF0, 0x0B, -1} }, { {0x83, -1}, {0xF0, 0x83, -1} }, { {0x0A, -1}, {0xF0, 0x0A, -1} }, { {0x01, -1}, {0xF0, 0x01, -1} },
        { {0x09, -1}, {0xF0, 0x09, -1} }, { {0x77, -1}, {0xF0, 0x77, -1} }, { {0x7E, -1}, {0xF0, 0x7E, -1} }, { {0x6C, -1}, {0xF0, 0x6C, -1} },
        { {0x75, -1}, {0xF0, 0x75, -1} }, { {0x7D, -1}, {0xF0, 0x7D, -1} }, { {0x7B, -1}, {0xF0, 0x7B, -1} }, { {0x6B, -1}, {0xF0, 0x6B, -1} },
        { {0x73, -1}, {0xF0, 0x73, -1} }, { {0x74, -1}, {0xF0, 0x74, -1} }, { {0x79, -1}, {0xF0, 0x79, -1} }, { {0x69, -1}, {0xF0, 0x69, -1} },
        { {0x72, -1}, {0xF0, 0x72, -1} }, { {0x7A, -1}, {0xF0, 0x7A, -1} }, { {0x70, -1}, {0xF0, 0x70, -1} }, { {0x71, -1}, {0xF0, 0x71, -1} },
        { {0x84, -1}, {0xF0, 0x84, -1} }, { {0x60, -1}, {0xF0, 0x60, -1} }, { {0x61, -1}, {0xF0, 0x61, -1} }, { {0x78, -1}, {0xF0, 0x78, -1} },  /*54*/
        { {0x07, -1}, {0xF0, 0x07, -1} }, { {0x0F, -1}, {0xF0, 0x0F, -1} }, { {0x17, -1}, {0xF0, 0x17, -1} }, { {0x1F, -1}, {0xF0, 0x1F, -1} },  /*58*/
        { {0x27, -1}, {0xF0, 0x27, -1} }, { {0x2F, -1}, {0xF0, 0x2F, -1} }, { {0x37, -1}, {0xF0, 0x37, -1} }, { {0x3F, -1}, {0xF0, 0x3F, -1} },  /*5c*/
        { {0x47, -1}, {0xF0, 0x47, -1} }, { {0x4F, -1}, {0xF0, 0x4F, -1} }, { {0x56, -1}, {0xF0, 0x56, -1} }, { {0x5E, -1}, {0xF0, 0x5E, -1} },  /*60*/
        { {0x08, -1}, {0xF0, 0x08, -1} }, { {0x10, -1}, {0xF0, 0x10, -1} }, { {0x18, -1}, {0xF0, 0x18, -1} }, { {0x20, -1}, {0xF0, 0x20, -1} },  /*64*/
        { {0x28, -1}, {0xF0, 0x28, -1} }, { {0x30, -1}, {0xF0, 0x30, -1} }, { {0x38, -1}, {0xF0, 0x38, -1} }, { {0x40, -1}, {0xF0, 0x40, -1} },  /*68*/
        { {0x48, -1}, {0xF0, 0x48, -1} }, { {0x50, -1}, {0xF0, 0x50, -1} }, { {0x57, -1}, {0xF0, 0x57, -1} }, { {0x6F, -1}, {0xF0, 0x6F, -1} },  /*6c*/
        { {0x13, -1}, {0xF0, 0x13, -1} }, { {0x19, -1}, {0xF0, 0x19, -1} }, { {0x39, -1}, {0xF0, 0x39, -1} }, { {0x51, -1}, {0xF0, 0x51, -1} },  /*70*/
        { {0x53, -1}, {0xF0, 0x53, -1} }, { {0x5C, -1}, {0xF0, 0x5C, -1} }, { {0x5F, -1}, {0xF0, 0x5F, -1} }, { {0x62, -1}, {0xF0, 0x62, -1} },  /*74*/
        { {0x63, -1}, {0xF0, 0x63, -1} }, { {0x64, -1}, {0xF0, 0x64, -1} }, { {0x65, -1}, {0xF0, 0x65, -1} }, { {0x67, -1}, {0xF0, 0x67, -1} },  /*78*/
        { {0x68, -1}, {0xF0, 0x68, -1} }, { {0x6A, -1}, {0xF0, 0x6A, -1} }, { {0x6D, -1}, {0xF0, 0x6D, -1} }, { {0x6E, -1}, {0xF0, 0x6E, -1} },  /*7c*/

        { {0x80, -1},       {0xF0, 0x80, -1} },       { {0x81, -1},       {0xF0, 0x81, -1} },       { {0x82, -1},       {0xF0, 0x82, -1} },       { {0xe0, 0x1E, -1}, {0xe0, 0xF0, 0x1E, -1} },                    /*80*/
        { {0xe0, 0x26, -1}, {0xe0, 0xF0, 0x26, -1} }, { {0x85, -1},       {0xF0, 0x85, -1} },       { {0x86, -1},       {0xF0, 0x86, -1} },       { {0x87, -1},       {0xF0, 0x87, -1} },                          /*84*/
        { {0xe0, 0x3D, -1}, {0xe0, 0xF0, 0x3D, -1} }, { {0xe0, 0x3E, -1}, {0xe0, 0xF0, 0x3E, -1} }, { {0xe0, 0x46, -1}, {0xe0, 0xF0, 0x46, -1} }, { {0xe0, 0x45, -1}, {0xe0, 0xF0, 0x45, -1} },                    /*88*/
        { {0xe0, 0x4E, -1}, {0xe0, 0xF0, 0x4E, -1} }, { {-1},             {-1} },                   { {0xe0, 0x66, -1}, {0xe0, 0xF0, 0x66, -1} }, { {0xe0, 0x0D, -1}, {0xe0, 0xF0, 0x0D, -1} },                    /*8c*/
        { {0xe0, 0x15, -1}, {0xe0, 0xF0, 0x15, -1} }, { {0xe0, 0x1D, -1}, {0xe0, 0xF0, 0x1D, -1} }, { {0xe0, 0x24, -1}, {0xe0, 0xF0, 0x24, -1} }, { {0xe0, 0x2D, -1}, {0xe0, 0xF0, 0x2D, -1} },                    /*90*/
        { {0xe0, 0x2C, -1}, {0xe0, 0xF0, 0x2C, -1} }, { {0xe0, 0x35, -1}, {0xe0, 0xF0, 0x35, -1} }, { {0xe0, 0x3C, -1}, {0xe0, 0xF0, 0x3C, -1} }, { {0xe0, 0x43, -1}, {0xe0, 0xF0, 0x43, -1} },                    /*94*/
        { {0xe0, 0x44, -1}, {0xe0, 0xF0, 0x44, -1} }, { {0xe0, 0x4D, -1}, {0xe0, 0xF0, 0x4D, -1} }, { {0xe0, 0x54, -1}, {0xe0, 0xF0, 0x54, -1} }, { {0xe0, 0x5B, -1}, {0xe0, 0xF0, 0x5B, -1} },                    /*98*/
        { {0xe0, 0x5A, -1}, {0xe0, 0xF0, 0x5A, -1} }, { {0xe0, 0x14, -1}, {0xe0, 0xF0, 0x14, -1} }, { {0xe0, 0x1C, -1}, {0xe0, 0xF0, 0x1C, -1} }, { {0xe0, 0x1B, -1}, {0xe0, 0xF0, 0x1B, -1} },                    /*9c*/
        { {0xe0, 0x23, -1}, {0xe0, 0xF0, 0x23, -1} }, { {0xe0, 0x2B, -1}, {0xe0, 0xF0, 0x2B, -1} }, { {0xe0, 0x34, -1}, {0xe0, 0xF0, 0x34, -1} }, { {0xe0, 0x33, -1}, {0xe0, 0xF0, 0x33, -1} },                    /*a0*/
        { {0xe0, 0x3B, -1}, {0xe0, 0xF0, 0x3B, -1} }, { {0xe0, 0x42, -1}, {0xe0, 0xF0, 0x42, -1} }, { {0xe0, 0x4B, -1}, {0xe0, 0xF0, 0x4B, -1} }, { {-1},             {-1} },                                      /*a4*/
        { {-1},             {-1} },                   { {-1},             {-1} },                   { {-1},             {-1} },                   { {-1},             {-1} },                                      /*a8*/
        { {0xe0, 0x1A, -1}, {0xe0, 0xF0, 0x1A, -1} }, { {0xe0, 0x22, -1}, {0xe0, 0xF0, 0x22, -1} }, { {0xe0, 0x21, -1}, {0xe0, 0xF0, 0x21, -1} }, { {0xe0, 0x2A, -1}, {0xe0, 0xF0, 0x2A, -1} },                    /*ac*/
        { {0xe0, 0x32, -1}, {0xe0, 0xF0, 0x32, -1} }, { {0xe0, 0x31, -1}, {0xe0, 0xF0, 0x31, -1} }, { {0xe0, 0x3A, -1}, {0xe0, 0xF0, 0x3A, -1} }, { {-1},             {-1} },                                      /*b0*/
        { {0xe0, 0x49, -1}, {0xe0, 0xF0, 0x49, -1} }, { {0xe0, 0x4A, -1}, {0xe0, 0xF0, 0x4A, -1} }, { {-1},             {-1} },                   { {0xe0, 0x7C, -1}, {0xe0, 0xF0, 0x7C, -1} },                    /*b4*/
        { {0xe0, 0x11, -1}, {0xe0, 0xF0, 0x11, -1} }, { {-1},             {-1} },                   { {0xe0, 0x58, -1}, {0xe0, 0xF0, 0x58, -1} }, { {0xe0, 0x05, -1}, {0xe0, 0xF0, 0x05, -1} },                    /*b8*/
        { {0xe0, 0x06, -1}, {0xe0, 0xF0, 0x06, -1} }, { {0xe0, 0x04, -1}, {0xe0, 0xF0, 0x04, -1} }, { {0xe0, 0x0C, -1}, {0xe0, 0xF0, 0x0C, -1} }, { {0xe0, 0x03, -1}, {0xe0, 0xF0, 0x03, -1} },                    /*bc*/
        { {0xe0, 0x0B, -1}, {0xe0, 0xF0, 0x0B, -1} }, { {0xe0, 0x02, -1}, {0xe0, 0xF0, 0x02, -1} }, { {0xe0, 0x0A, -1}, {0xe0, 0xF0, 0x0A, -1} }, { {0xe0, 0x01, -1}, {0xe0, 0xF0, 0x01, -1} },                    /*c0*/
        { {0xe0, 0x09, -1}, {0xe0, 0xF0, 0x09, -1} }, { {-1},             {-1} },                   { {0xe0, 0x7E, -1}, {0xe0, 0xF0, 0x7E, -1} }, { {0xe0, 0xf0, 0x12, 0xe0, 0x6C, -1}, {0xe0, 0xF0, 0x6C, 0xe0, 0x12, -1} },                    /*c4*/
        { {0xe0, 0xf0, 0x12, 0xe0, 0x75, -1}, {0xe0, 0xF0, 0x75, 0xe0, 0x12, -1} }, { {0xe0, 0xf0, 0x12, 0xe0, 0x7D, -1}, {0xe0, 0xF0, 0x7D, 0xe0, 0x12, -1} }, { {-1},             {-1} },                   { {0xe0, 0xf0, 0x12, 0xe0, 0x6B, -1}, {0xe0, 0xF0, 0x6B, 0xe0, 0x12, -1} },                    /*c8*/
        { {0xe0, 0x73, -1}, {0xe0, 0xF0, 0x73, -1} }, { {0xe0, 0xf0, 0x12, 0xe0, 0x74, -1}, {0xe0, 0xF0, 0x74, 0xe0, 0x12, -1} }, { {0xe0, 0x79, -1}, {0xe0, 0xF0, 0x79, -1} }, { {0xe0, 0xf0, 0x12, 0xe0, 0x69, -1}, {0xe0, 0xF0, 0x69, 0xe0, 0x12, -1} },                    /*cc*/
        { {0xe0, 0xf0, 0x12, 0xe0, 0x72, -1}, {0xe0, 0xF0, 0x72, 0xe0, 0x12, -1} }, { {0xe0, 0xf0, 0x12, 0xe0, 0x7A, -1}, {0xe0, 0xF0, 0x7A, 0xe0, 0x12, -1} }, { {0xe0, 0xf0, 0x12, 0xe0, 0x70, -1}, {0xe0, 0xF0, 0x70, 0xe0, 0x12, -1} }, { {0xe0, 0xf0, 0x12, 0xe0, 0x71, -1}, {0xe0, 0xF0, 0x71, 0xe0, 0x12, -1} },                    /*d0*/
        { {0xd4, -1},       {0xF0, 0xD4, -1} },       { {0xe0, 0x60, -1}, {0xe0, 0xF0, 0x60, -1} }, { {-1},             {-1} },                   { {0xe0, 0x78, -1}, {0xe0, 0xF0, 0x78, -1} },                    /*d4*/
        { {0xe0, 0x07, -1}, {0xe0, 0xF0, 0x07, -1} }, { {0xe0, 0x0F, -1}, {0xe0, 0xF0, 0x0F, -1} }, { {0xe0, 0x17, -1}, {0xe0, 0xF0, 0x17, -1} }, { {0xe0, 0x1F, -1}, {0xe0, 0xF0, 0x1F, -1} },                    /*d8*/
        { {0xe0, 0x27, -1}, {0xe0, 0xF0, 0x27, -1} }, { {0xe0, 0x2F, -1}, {0xe0, 0xF0, 0x2F, -1} }, { {0xe0, 0x37, -1}, {0xe0, 0xF0, 0x37, -1} }, { {0xe0, 0x3F, -1}, {0xe0, 0xF0, 0x3F, -1} },                    /*dc*/
        { {-1},             {-1} },                   { {0xe0, 0x4F, -1}, {0xe0, 0xF0, 0x4F, -1} }, { {0xe0, 0x56, -1}, {0xe0, 0xF0, 0x56, -1} }, { {0xe0, 0x5E, -1}, {0xe0, 0xF0, 0x5E, -1} },                    /*e0*/
        { {0xe0, 0x08, -1}, {0xe0, 0xF0, 0x08, -1} }, { {0xe0, 0x10, -1}, {0xe0, 0xF0, 0x10, -1} }, { {0xe0, 0x18, -1}, {0xe0, 0xF0, 0x18, -1} }, { {0xe0, 0x20, -1}, {0xe0, 0xF0, 0x20, -1} },                    /*e4*/
        { {0xe0, 0x28, -1}, {0xe0, 0xF0, 0x28, -1} }, { {0xe0, 0x30, -1}, {0xe0, 0xF0, 0x30, -1} }, { {0xe0, 0x38, -1}, {0xe0, 0xF0, 0x38, -1} }, { {0xe0, 0x40, -1}, {0xe0, 0xF0, 0x40, -1} },                    /*e8*/
        { {0xe0, 0x48, -1}, {0xe0, 0xF0, 0x48, -1} }, { {0xe0, 0x50, -1}, {0xe0, 0xF0, 0x50, -1} }, { {0xe0, 0x57, -1}, {0xe0, 0xF0, 0x57, -1} }, { {-1},             {-1} },                                      /*ec*/
        { {0xe0, 0x13, -1}, {0xe0, 0xF0, 0x13, -1} }, { {0xf1, -1},       {0xF0, 0xF1, -1} },       { {0xf2, -1},       {0xF0, 0xF2, -1} },       { {0xe0, 0x51, -1}, {0xe0, 0xF0, 0x51, -1} },                    /*f0*/
        { {0xe0, 0x53, -1}, {0xe0, 0xF0, 0x53, -1} }, { {0xe0, 0x5C, -1}, {0xe0, 0xF0, 0x5C, -1} }, { {-1},             {-1} },                   { {0xe0, 0x62, -1}, {0xe0, 0xF0, 0x62, -1} },                    /*f4*/
        { {0xe0, 0x63, -1}, {0xe0, 0xF0, 0x63, -1} }, { {0xe0, 0x64, -1}, {0xe0, 0xF0, 0x64, -1} }, { {0xe0, 0x65, -1}, {0xe0, 0xF0, 0x65, -1} }, { {0xe0, 0x67, -1}, {0xe0, 0xF0, 0x67, -1} },                    /*f8*/
        { {0xe0, 0x68, -1}, {0xe0, 0xF0, 0x68, -1} }, { {0xe0, 0x6A, -1}, {0xe0, 0xF0, 0x6A, -1} }, { {0xe0, 0x6D, -1}, {0xe0, 0xF0, 0x6D, -1} }, { {0xe1, 0x14, -1}, {0xe1, 0xf0, 0x14, -1} },  /*fc*/

        { {-1},             {-1} },                   { {0xe0, 0x76, -1}, {0xe0, 0xF0, 0x76, -1} }, { {0xe0, 0x16, -1}, {0xe0, 0xF0, 0x16, -1} }, { {0xe0, 0xf0, 0x12, -1}, {0xe0, 0x12, -1} },  /*100*/
        { {-1},             {-1} },                   { {0xe0, 0x25, -1}, {0xe0, 0xF0, 0x25, -1} }, { {0xe0, 0x2E, -1}, {0xe0, 0xF0, 0x2E, -1} }, { {0xe0, 0x36, -1}, {0xe0, 0xF0, 0x36, -1} },  /*104*/
        { {0xe0, 0x19, -1}, {0xe0, 0xF0, 0x19, -1} }, { {0xe0, 0x39, -1}, {0xe0, 0xF0, 0x39, -1} }, { {0xe0, 0x6E, -1}, {0xe0, 0xF0, 0x6E, -1} }, { {0xe0, 0xe1, -1}, {0xe0, 0xF0, 0xE1, -1} },  /*108*/
        { {0xe0, 0xee, -1}, {0xe0, 0xF0, 0xEE, -1} }, { {0xe0, 0xf1, -1}, {0xe0, 0xF0, 0xF1, -1} }, { {0xe0, 0xfe, -1}, {0xe0, 0xF0, 0xFE, -1} }, { {0xe0, 0xff, -1}, {0xe0, 0xF0, 0xFF, -1} }   /*10c*/
};

static scancode scancode_set3[272] =
{
        { {-1},       {-1} },             { {0x08, -1}, {0xF0, 0x08, -1} }, { {0x16, -1}, {0xF0, 0x16, -1} }, { {0x1E, -1}, {0xF0, 0x1E, -1} },
        { {0x26, -1}, {0xF0, 0x26, -1} }, { {0x25, -1}, {0xF0, 0x25, -1} }, { {0x2E, -1}, {0xF0, 0x2E, -1} }, { {0x36, -1}, {0xF0, 0x36, -1} },
        { {0x3D, -1}, {0xF0, 0x3D, -1} }, { {0x3E, -1}, {0xF0, 0x3E, -1} }, { {0x46, -1}, {0xF0, 0x46, -1} }, { {0x45, -1}, {0xF0, 0x45, -1} },
        { {0x4E, -1}, {0xF0, 0x4E, -1} }, { {0x55, -1}, {0xF0, 0x55, -1} }, { {0x66, -1}, {0xF0, 0x66, -1} }, { {0x0D, -1}, {0xF0, 0x0D, -1} },
        { {0x15, -1}, {0xF0, 0x15, -1} }, { {0x1D, -1}, {0xF0, 0x1D, -1} }, { {0x24, -1}, {0xF0, 0x24, -1} }, { {0x2D, -1}, {0xF0, 0x2D, -1} },
        { {0x2C, -1}, {0xF0, 0x2C, -1} }, { {0x35, -1}, {0xF0, 0x35, -1} }, { {0x3C, -1}, {0xF0, 0x3C, -1} }, { {0x43, -1}, {0xF0, 0x43, -1} },
        { {0x44, -1}, {0xF0, 0x44, -1} }, { {0x4D, -1}, {0xF0, 0x4D, -1} }, { {0x54, -1}, {0xF0, 0x54, -1} }, { {0x5B, -1}, {0xF0, 0x5B, -1} },
        { {0x5A, -1}, {0xF0, 0x5A, -1} }, { {0x11, -1}, {0xF0, 0x11, -1} }, { {0x1C, -1}, {0xF0, 0x1C, -1} }, { {0x1B, -1}, {0xF0, 0x1B, -1} },
        { {0x23, -1}, {0xF0, 0x23, -1} }, { {0x2B, -1}, {0xF0, 0x2B, -1} }, { {0x34, -1}, {0xF0, 0x34, -1} }, { {0x33, -1}, {0xF0, 0x33, -1} },
        { {0x3B, -1}, {0xF0, 0x3B, -1} }, { {0x42, -1}, {0xF0, 0x42, -1} }, { {0x4B, -1}, {0xF0, 0x4B, -1} }, { {0x4C, -1}, {0xF0, 0x4C, -1} },
        { {0x52, -1}, {0xF0, 0x52, -1} }, { {0x0E, -1}, {0xF0, 0x0E, -1} }, { {0x12, -1}, {0xF0, 0x12, -1} }, { {0x5D, -1}, {0xF0, 0x5D, -1} },
        { {0x1A, -1}, {0xF0, 0x1A, -1} }, { {0x22, -1}, {0xF0, 0x22, -1} }, { {0x21, -1}, {0xF0, 0x21, -1} }, { {0x2A, -1}, {0xF0, 0x2A, -1} },
        { {0x32, -1}, {0xF0, 0x32, -1} }, { {0x31, -1}, {0xF0, 0x31, -1} }, { {0x3A, -1}, {0xF0, 0x3A, -1} }, { {0x41, -1}, {0xF0, 0x41, -1} },
        { {0x49, -1}, {0xF0, 0x49, -1} }, { {0x4A, -1}, {0xF0, 0x4A, -1} }, { {0x59, -1}, {0xF0, 0x59, -1} }, { {0x7E, -1}, {0xF0, 0x7E, -1} },
        { {0x19, -1}, {0xF0, 0x19, -1} }, { {0x29, -1}, {0xF0, 0x29, -1} }, { {0x14, -1}, {0xF0, 0x14, -1} }, { {0x07, -1}, {0xF0, 0x07, -1} },
        { {0x0F, -1}, {0xF0, 0x0F, -1} }, { {0x17, -1}, {0xF0, 0x17, -1} }, { {0x1F, -1}, {0xF0, 0x1F, -1} }, { {0x27, -1}, {0xF0, 0x27, -1} },
        { {0x2F, -1}, {0xF0, 0x2F, -1} }, { {0x37, -1}, {0xF0, 0x37, -1} }, { {0x3F, -1}, {0xF0, 0x3F, -1} }, { {0x47, -1}, {0xF0, 0x47, -1} },
        { {0x4F, -1}, {0xF0, 0x4F, -1} }, { {0x76, -1}, {0xF0, 0x76, -1} }, { {0x5F, -1}, {0xF0, 0x5F, -1} }, { {0x6C, -1}, {0xF0, 0x6C, -1} },
        { {0x75, -1}, {0xF0, 0x75, -1} }, { {0x7D, -1}, {0xF0, 0x7D, -1} }, { {0x84, -1}, {0xF0, 0x84, -1} }, { {0x6B, -1}, {0xF0, 0x6B, -1} },
        { {0x73, -1}, {0xF0, 0x73, -1} }, { {0x74, -1}, {0xF0, 0x74, -1} }, { {0x7C, -1}, {0xF0, 0x7C, -1} }, { {0x69, -1}, {0xF0, 0x69, -1} },
        { {0x72, -1}, {0xF0, 0x72, -1} }, { {0x7A, -1}, {0xF0, 0x7A, -1} }, { {0x70, -1}, {0xF0, 0x70, -1} }, { {0x71, -1}, {0xF0, 0x71, -1} },
        { {0x57, -1}, {0xF0, 0x57, -1} }, { {0x60, -1}, {0xF0, 0x60, -1} }, { {-1},       {-1} },             { {0x56, -1}, {0xF0, 0x56, -1} },
        { {0x5E, -1}, {0xF0, 0x5E, -1} }, { {-1},       {-1} },             { {-1},       {-1} },             { {-1},       {-1} },
        { {-1},       {-1} },             { {-1},       {-1} },             { {-1},       {-1} },             { {-1},       {-1} },
        { {-1},       {-1} },             { {-1},       {-1} },             { {-1},       {-1} },             { {-1},       {-1} },
        { {-1},       {-1} },             { {0x10, -1}, {0xF0, 0x10, -1} }, { {0x18, -1}, {0xF0, 0x18, -1} }, { {0x20, -1}, {0xF0, 0x20, -1} },
        { {0x28, -1}, {0xF0, 0x28, -1} }, { {0x30, -1}, {0xF0, 0x30, -1} }, { {0x38, -1}, {0xF0, 0x38, -1} }, { {0x40, -1}, {0xF0, 0x40, -1} },
        { {0x48, -1}, {0xF0, 0x48, -1} }, { {0x50, -1}, {0xF0, 0x50, -1} }, { {-1},       {-1} },             { {-1},       {-1} },
        { {0x87, -1}, {0xF0, 0x87, -1} }, { {-1},       {-1} },             { {-1},       {-1} },             { {0x51, -1}, {0xF0, 0x51, -1} },
        { {0x53, -1}, {0xF0, 0x53, -1} }, { {0x5C, -1}, {0xF0, 0x5C, -1} }, { {-1},       {-1} },             { {0x62, -1}, {0xF0, 0x62, -1} },
        { {0x63, -1}, {0xF0, 0x63, -1} }, { {0x86, -1}, {0xF0, 0x86, -1} }, { {-1},       {-1} },             { {0x85, -1}, {0xF0, 0x85, -1} },
        { {0x68, -1}, {0xF0, 0x68, -1} }, { {0x13, -1}, {0xF0, 0x13, -1} }, { {-1},       {-1} },             { {-1},       {-1} },

        { {0x80, -1},       {0xF0, 0x80, -1} },       { {0x81, -1},       {0xF0, 0x81, -1} },       { {0x82, -1},       {0xF0, 0x82, -1} },       { {0xe0, 0x1E, -1}, {0xe0, 0xF0, 0x1E, -1} },  /*80*/
        { {0xe0, 0x26, -1}, {0xe0, 0xF0, 0x26, -1} }, { {0x85, -1},       {0xF0, 0x85, -1} },       { {0x86, -1},       {0xF0, 0x86, -1} },       { {0x87, -1},       {0xF0, 0x87, -1} },        /*84*/
        { {0xe0, 0x3D, -1}, {0xe0, 0xF0, 0x3D, -1} }, { {0xe0, 0x3E, -1}, {0xe0, 0xF0, 0x3E, -1} }, { {0xe0, 0x46, -1}, {0xe0, 0xF0, 0x46, -1} }, { {0xe0, 0x45, -1}, {0xe0, 0xF0, 0x45, -1} },  /*88*/
        { {0xe0, 0x4E, -1}, {0xe0, 0xF0, 0x4E, -1} }, { {-1},             {-1} },                   { {0xe0, 0x66, -1}, {0xe0, 0xF0, 0x66, -1} }, { {0xe0, 0x0D, -1}, {0xe0, 0xF0, 0x0D, -1} },  /*8c*/
        { {0xe0, 0x15, -1}, {0xe0, 0xF0, 0x15, -1} }, { {0xe0, 0x1D, -1}, {0xe0, 0xF0, 0x1D, -1} }, { {0xe0, 0x24, -1}, {0xe0, 0xF0, 0x24, -1} }, { {0xe0, 0x2D, -1}, {0xe0, 0xF0, 0x2D, -1} },  /*90*/
        { {0xe0, 0x2C, -1}, {0xe0, 0xF0, 0x2C, -1} }, { {0xe0, 0x35, -1}, {0xe0, 0xF0, 0x35, -1} }, { {0xe0, 0x3C, -1}, {0xe0, 0xF0, 0x3C, -1} }, { {0xe0, 0x43, -1}, {0xe0, 0xF0, 0x43, -1} },  /*94*/
        { {0xe0, 0x44, -1}, {0xe0, 0xF0, 0x44, -1} }, { {0xe0, 0x4D, -1}, {0xe0, 0xF0, 0x4D, -1} }, { {0xe0, 0x54, -1}, {0xe0, 0xF0, 0x54, -1} }, { {0xe0, 0x5B, -1}, {0xe0, 0xF0, 0x5B, -1} },  /*98*/
        { {0x79, -1},       {0xF0, 0x79, -1} },       { {0x58, -1},       {0xF0, 0x58, -1} },       { {0xe0, 0x1C, -1}, {0xe0, 0xF0, 0x1C, -1} }, { {0xe0, 0x1B, -1}, {0xe0, 0xF0, 0x1B, -1} },  /*9c*/
        { {0xe0, 0x23, -1}, {0xe0, 0xF0, 0x23, -1} }, { {0xe0, 0x2B, -1}, {0xe0, 0xF0, 0x2B, -1} }, { {0xe0, 0x34, -1}, {0xe0, 0xF0, 0x34, -1} }, { {0xe0, 0x33, -1}, {0xe0, 0xF0, 0x33, -1} },  /*a0*/
        { {0xe0, 0x3B, -1}, {0xe0, 0xF0, 0x3B, -1} }, { {0xe0, 0x42, -1}, {0xe0, 0xF0, 0x42, -1} }, { {0xe0, 0x4B, -1}, {0xe0, 0xF0, 0x4B, -1} }, { {-1},             {-1} },                    /*a4*/
        { {-1},             {-1} },                   { {-1},             {-1} },                   { {-1},             {-1} },                   { {-1},             {-1} },                    /*a8*/
        { {0xe0, 0x1A, -1}, {0xe0, 0xF0, 0x1A, -1} }, { {0xe0, 0x22, -1}, {0xe0, 0xF0, 0x22, -1} }, { {0xe0, 0x21, -1}, {0xe0, 0xF0, 0x21, -1} }, { {0xe0, 0x2A, -1}, {0xe0, 0xF0, 0x2A, -1} },  /*ac*/
        { {0xe0, 0x32, -1}, {0xe0, 0xF0, 0x32, -1} }, { {0xe0, 0x31, -1}, {0xe0, 0xF0, 0x31, -1} }, { {0xe0, 0x3A, -1}, {0xe0, 0xF0, 0x3A, -1} }, { {-1},             {-1} },                    /*b0*/
        { {0xe0, 0x49, -1}, {0xe0, 0xF0, 0x49, -1} }, { {0x77, -1},       {0xF0, 0x77, -1} },       { {-1},             {-1} },                   { {0x57, -1},       {0xF0, 0x57, -1} },        /*b4*/
        { {0x39, -1},       {0xF0, 0x39, -1} },       { {-1},             {-1} },                   { {0xe0, 0x58, -1}, {0xe0, 0xF0, 0x58, -1} }, { {0xe0, 0x05, -1}, {0xe0, 0xF0, 0x05, -1} },  /*b8*/
        { {0xe0, 0x06, -1}, {0xe0, 0xF0, 0x06, -1} }, { {0xe0, 0x04, -1}, {0xe0, 0xF0, 0x04, -1} }, { {0xe0, 0x0C, -1}, {0xe0, 0xF0, 0x0C, -1} }, { {0xe0, 0x03, -1}, {0xe0, 0xF0, 0x03, -1} },  /*bc*/
        { {0xe0, 0x0B, -1}, {0xe0, 0xF0, 0x0B, -1} }, { {0xe0, 0x02, -1}, {0xe0, 0xF0, 0x02, -1} }, { {0xe0, 0x0A, -1}, {0xe0, 0xF0, 0x0A, -1} }, { {0xe0, 0x01, -1}, {0xe0, 0xF0, 0x01, -1} },  /*c0*/
        { {0xe0, 0x09, -1}, {0xe0, 0xF0, 0x09, -1} }, { {-1},             {-1} },                   { {0xe0, 0x7E, -1}, {0xe0, 0xF0, 0x7E, -1} }, { {0x6E, -1},       {0xF0, 0x6E, -1} },        /*c4*/
        { {0x63, -1},       {0xF0, 0x63, -1} },       { {0x6F, -1},       {0xF0, 0x6F, -1} },       { {-1},             {-1} },                   { {0x61, -1},       {0xF0, 0x61, -1} },        /*c8*/
        { {0xe0, 0x73, -1}, {0xe0, 0xF0, 0x73, -1} }, { {0x6A, -1},       {0xF0, 0x6A, -1} },       { {0xe0, 0x79, -1}, {0xe0, 0xF0, 0x79, -1} }, { {0x65, -1},       {0xF0, 0x65, -1} },        /*cc*/
        { {0x60, -1},       {0xe0, 0x60, -1} },       { {0x6D, -1},       {0xF0, 0x6D, -1} },       { {0x67, -1},       {0xF0, 0x67, -1} },       { {0x64, -1},       {0xF0, 0x64, -1} },        /*d0*/
        { {0xd4, -1},       {0xF0, 0xD4, -1} },       { {0xe0, 0x60, -1}, {0xe0, 0xF0, 0x60, -1} }, { {-1},             {-1} },                   { {0xe0, 0x78, -1}, {0xe0, 0xF0, 0x78, -1} },  /*d4*/
        { {0xe0, 0x07, -1}, {0xe0, 0xF0, 0x07, -1} }, { {0xe0, 0x0F, -1}, {0xe0, 0xF0, 0x0F, -1} }, { {0xe0, 0x17, -1}, {0xe0, 0xF0, 0x17, -1} }, { {0x8B, -1},       {0xF0, 0x8B, -1} },        /*d8*/
        { {0x8C, -1},       {0xF0, 0x8C, -1} },       { {0x8D, -1},       {0xF0, 0x8D, -1} },       { {-1},             {-1} },                   { {0x7F, -1},       {0xF0, 0x7F, -1} },        /*dc*/
        { {-1},             {-1} },                   { {0xe0, 0x4F, -1}, {0xe0, 0xF0, 0x4F, -1} }, { {0xe0, 0x56, -1}, {0xe0, 0xF0, 0x56, -1} }, { {-1},             {-1} },                    /*e0*/
        { {0xe0, 0x08, -1}, {0xe0, 0xF0, 0x08, -1} }, { {0xe0, 0x10, -1}, {0xe0, 0xF0, 0x10, -1} }, { {0xe0, 0x18, -1}, {0xe0, 0xF0, 0x18, -1} }, { {0xe0, 0x20, -1}, {0xe0, 0xF0, 0x20, -1} },  /*e4*/
        { {0xe0, 0x28, -1}, {0xe0, 0xF0, 0x28, -1} }, { {0xe0, 0x30, -1}, {0xe0, 0xF0, 0x30, -1} }, { {0xe0, 0x38, -1}, {0xe0, 0xF0, 0x38, -1} }, { {0xe0, 0x40, -1}, {0xe0, 0xF0, 0x40, -1} },  /*e8*/
        { {0xe0, 0x48, -1}, {0xe0, 0xF0, 0x48, -1} }, { {0xe0, 0x50, -1}, {0xe0, 0xF0, 0x50, -1} }, { {0xe0, 0x57, -1}, {0xe0, 0xF0, 0x57, -1} }, { {-1},             {-1} },                    /*ec*/
        { {0xe0, 0x13, -1}, {0xe0, 0xF0, 0x13, -1} }, { {0xf1, -1},       {0xF0, 0xF1, -1} },       { {0xf2, -1},       {0xF0, 0xF2, -1} },       { {0xe0, 0x51, -1}, {0xe0, 0xF0, 0x51, -1} },  /*f0*/
        { {0xe0, 0x53, -1}, {0xe0, 0xF0, 0x53, -1} }, { {0xe0, 0x5C, -1}, {0xe0, 0xF0, 0x5C, -1} }, { {-1},             {-1} },                   { {0xe0, 0x62, -1}, {0xe0, 0xF0, 0x62, -1} },  /*f4*/
        { {0xe0, 0x63, -1}, {0xe0, 0xF0, 0x63, -1} }, { {0xe0, 0x64, -1}, {0xe0, 0xF0, 0x64, -1} }, { {0xe0, 0x65, -1}, {0xe0, 0xF0, 0x65, -1} }, { {0xe0, 0x67, -1}, {0xe0, 0xF0, 0x67, -1} },  /*f8*/
        { {0xe0, 0x68, -1}, {0xe0, 0xF0, 0x68, -1} }, { {0xe0, 0x6A, -1}, {0xe0, 0xF0, 0x6A, -1} }, { {0xe0, 0x6D, -1}, {0xe0, 0xF0, 0x6D, -1} }, { {0x62, -1},       {0xF0, 0x62, -1} },        /*fc*/

        { {-1},             {-1} },                   { {0xe0, 0x76, -1}, {0xe0, 0xF0, 0x76, -1} }, { {0xe0, 0x16, -1}, {0xe0, 0xF0, 0x16, -1} }, { {-1},             {-1} },                    /*100*/
        { {-1},             {-1} },                   { {0xe0, 0x25, -1}, {0xe0, 0xF0, 0x25, -1} }, { {0xe0, 0x2E, -1}, {0xe0, 0xF0, 0x2E, -1} }, { {0xe0, 0x36, -1}, {0xe0, 0xF0, 0x36, -1} },  /*104*/
        { {0xe0, 0x19, -1}, {0xe0, 0xF0, 0x19, -1} }, { {0xe0, 0x39, -1}, {0xe0, 0xF0, 0x39, -1} }, { {0xe0, 0x6E, -1}, {0xe0, 0xF0, 0x6E, -1} }, { {0xe0, 0xe1, -1}, {0xe0, 0xF0, 0xE1, -1} },  /*108*/
        { {0xe0, 0xee, -1}, {0xe0, 0xF0, 0xEE, -1} }, { {0xe0, 0xf1, -1}, {0xe0, 0xF0, 0xF1, -1} }, { {0xe0, 0xfe, -1}, {0xe0, 0xF0, 0xFE, -1} }, { {0xe0, 0xff, -1}, {0xe0, 0xF0, 0xFF, -1} }   /*10c*/
};

/*XT keyboard has no escape scancodes, and no scancodes beyond 53*/
static scancode scancode_xt[272] =
{
        { {-1},       {-1} },       { {0x01, -1}, {0x81, -1} }, { {0x02, -1}, {0x82, -1} }, { {0x03, -1}, {0x83, -1} },
        { {0x04, -1}, {0x84, -1} }, { {0x05, -1}, {0x85, -1} }, { {0x06, -1}, {0x86, -1} }, { {0x07, -1}, {0x87, -1} },
        { {0x08, -1}, {0x88, -1} }, { {0x09, -1}, {0x89, -1} }, { {0x0a, -1}, {0x8a, -1} }, { {0x0b, -1}, {0x8b, -1} },
        { {0x0c, -1}, {0x8c, -1} }, { {0x0d, -1}, {0x8d, -1} }, { {0x0e, -1}, {0x8e, -1} }, { {0x0f, -1}, {0x8f, -1} },
        { {0x10, -1}, {0x90, -1} }, { {0x11, -1}, {0x91, -1} }, { {0x12, -1}, {0x92, -1} }, { {0x13, -1}, {0x93, -1} },
        { {0x14, -1}, {0x94, -1} }, { {0x15, -1}, {0x95, -1} }, { {0x16, -1}, {0x96, -1} }, { {0x17, -1}, {0x97, -1} },
        { {0x18, -1}, {0x98, -1} }, { {0x19, -1}, {0x99, -1} }, { {0x1a, -1}, {0x9a, -1} }, { {0x1b, -1}, {0x9b, -1} },
        { {0x1c, -1}, {0x9c, -1} }, { {0x1d, -1}, {0x9d, -1} }, { {0x1e, -1}, {0x9e, -1} }, { {0x1f, -1}, {0x9f, -1} },
        { {0x20, -1}, {0xa0, -1} }, { {0x21, -1}, {0xa1, -1} }, { {0x22, -1}, {0xa2, -1} }, { {0x23, -1}, {0xa3, -1} },
        { {0x24, -1}, {0xa4, -1} }, { {0x25, -1}, {0xa5, -1} }, { {0x26, -1}, {0xa6, -1} }, { {0x27, -1}, {0xa7, -1} },
        { {0x28, -1}, {0xa8, -1} }, { {0x29, -1}, {0xa9, -1} }, { {0x2a, -1}, {0xaa, -1} }, { {0x2b, -1}, {0xab, -1} },
        { {0x2c, -1}, {0xac, -1} }, { {0x2d, -1}, {0xad, -1} }, { {0x2e, -1}, {0xae, -1} }, { {0x2f, -1}, {0xaf, -1} },
        { {0x30, -1}, {0xb0, -1} }, { {0x31, -1}, {0xb1, -1} }, { {0x32, -1}, {0xb2, -1} }, { {0x33, -1}, {0xb3, -1} },
        { {0x34, -1}, {0xb4, -1} }, { {0x35, -1}, {0xb5, -1} }, { {0x36, -1}, {0xb6, -1} }, { {0x37, -1}, {0xb7, -1} },
        { {0x38, -1}, {0xb8, -1} }, { {0x39, -1}, {0xb9, -1} }, { {0x3a, -1}, {0xba, -1} }, { {0x3b, -1}, {0xbb, -1} },
        { {0x3c, -1}, {0xbc, -1} }, { {0x3d, -1}, {0xbd, -1} }, { {0x3e, -1}, {0xbe, -1} }, { {0x3f, -1}, {0xbf, -1} },
        { {0x40, -1}, {0xc0, -1} }, { {0x41, -1}, {0xc1, -1} }, { {0x42, -1}, {0xc2, -1} }, { {0x43, -1}, {0xc3, -1} },
        { {0x44, -1}, {0xc4, -1} }, { {0x45, -1}, {0xc5, -1} }, { {0x46, -1}, {0xc6, -1} }, { {0x47, -1}, {0xc7, -1} },
        { {0x48, -1}, {0xc8, -1} }, { {0x49, -1}, {0xc9, -1} }, { {0x4a, -1}, {0xca, -1} }, { {0x4b, -1}, {0xcb, -1} },
        { {0x4c, -1}, {0xcc, -1} }, { {0x4d, -1}, {0xcd, -1} }, { {0x4e, -1}, {0xce, -1} }, { {0x4f, -1}, {0xcf, -1} },
        { {0x50, -1}, {0xd0, -1} }, { {0x51, -1}, {0xd1, -1} }, { {0x52, -1}, {0xd2, -1} }, { {0x53, -1}, {0xd3, -1} },
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*54*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*58*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*5c*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*60*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*64*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*68*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*6c*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*70*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*74*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*78*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*7c*/

        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*80*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*84*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*88*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*8c*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*90*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*94*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*98*/
        { {0x1c, -1}, {0x9c, -1} },             { {0x1d, -1}, {0x9d, -1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*9c*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*a0*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*a4*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {0xaa, -1}, {0x2a, -1} },             { {-1},             {-1} },             /*a8*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*ac*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*b0*/
        { {-1},             {-1} },             { {0x35, -1}, {0xb5, -1} },             { {0xb6, -1}, {0x36, -1} },             { {0x37, -1}, {0xb7, -1} }, /*b4*/
        { {0x38, -1}, {0xb8, -1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*b8*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*bc*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*c0*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {0x46, -1}, {0xc6, -1} },             { {0x47, -1}, {0xc7, -1} }, /*c4*/
        { {0x48, -1}, {0xc8, -1} },             { {0x49, -1}, {0xc9, -1} },             { {-1},             {-1} },             { {0x4b, -1}, {0xcb, -1} }, /*c8*/
        { {-1},             {-1} },             { {0x4d, -1}, {0xcd, -1} },             { {-1},             {-1} },             { {0x4f, -1}, {0xcf, -1} }, /*cc*/
        { {0x50, -1}, {0xd0, -1} },             { {0x51, -1}, {0xd1, -1} },             { {0x52, -1}, {0xd2, -1} },             { {0x53, -1}, {0xd3, -1} }, /*d0*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*d4*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*d8*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*dc*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*e0*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*e4*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*e8*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*ec*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*f0*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*f4*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*f8*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*fc*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*100*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*104*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*108*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*10c*/
};

/*Tandy keyboard has slightly different scancodes to XT*/
static scancode scancode_tandy[272] =
{
        { {-1},       {-1} },       { {0x01, -1}, {0x81, -1} }, { {0x02, -1}, {0x82, -1} }, { {0x03, -1}, {0x83, -1} },
        { {0x04, -1}, {0x84, -1} }, { {0x05, -1}, {0x85, -1} }, { {0x06, -1}, {0x86, -1} }, { {0x07, -1}, {0x87, -1} },
        { {0x08, -1}, {0x88, -1} }, { {0x09, -1}, {0x89, -1} }, { {0x0a, -1}, {0x8a, -1} }, { {0x0b, -1}, {0x8b, -1} },
        { {0x0c, -1}, {0x8c, -1} }, { {0x0d, -1}, {0x8d, -1} }, { {0x0e, -1}, {0x8e, -1} }, { {0x0f, -1}, {0x8f, -1} },
        { {0x10, -1}, {0x90, -1} }, { {0x11, -1}, {0x91, -1} }, { {0x12, -1}, {0x92, -1} }, { {0x13, -1}, {0x93, -1} },
        { {0x14, -1}, {0x94, -1} }, { {0x15, -1}, {0x95, -1} }, { {0x16, -1}, {0x96, -1} }, { {0x17, -1}, {0x97, -1} },
        { {0x18, -1}, {0x98, -1} }, { {0x19, -1}, {0x99, -1} }, { {0x1a, -1}, {0x9a, -1} }, { {0x1b, -1}, {0x9b, -1} },
        { {0x1c, -1}, {0x9c, -1} }, { {0x1d, -1}, {0x9d, -1} }, { {0x1e, -1}, {0x9e, -1} }, { {0x1f, -1}, {0x9f, -1} },
        { {0x20, -1}, {0xa0, -1} }, { {0x21, -1}, {0xa1, -1} }, { {0x22, -1}, {0xa2, -1} }, { {0x23, -1}, {0xa3, -1} },
        { {0x24, -1}, {0xa4, -1} }, { {0x25, -1}, {0xa5, -1} }, { {0x26, -1}, {0xa6, -1} }, { {0x27, -1}, {0xa7, -1} },
        { {0x28, -1}, {0xa8, -1} }, { {0x29, -1}, {0xa9, -1} }, { {0x2a, -1}, {0xaa, -1} }, { {0x47, -1}, {0xc7, -1} },
        { {0x2c, -1}, {0xac, -1} }, { {0x2d, -1}, {0xad, -1} }, { {0x2e, -1}, {0xae, -1} }, { {0x2f, -1}, {0xaf, -1} },
        { {0x30, -1}, {0xb0, -1} }, { {0x31, -1}, {0xb1, -1} }, { {0x32, -1}, {0xb2, -1} }, { {0x33, -1}, {0xb3, -1} },
        { {0x34, -1}, {0xb4, -1} }, { {0x35, -1}, {0xb5, -1} }, { {0x36, -1}, {0xb6, -1} }, { {0x37, -1}, {0xb7, -1} },
        { {0x38, -1}, {0xb8, -1} }, { {0x39, -1}, {0xb9, -1} }, { {0x3a, -1}, {0xba, -1} }, { {0x3b, -1}, {0xbb, -1} },
        { {0x3c, -1}, {0xbc, -1} }, { {0x3d, -1}, {0xbd, -1} }, { {0x3e, -1}, {0xbe, -1} }, { {0x3f, -1}, {0xbf, -1} },
        { {0x40, -1}, {0xc0, -1} }, { {0x41, -1}, {0xc1, -1} }, { {0x42, -1}, {0xc2, -1} }, { {0x43, -1}, {0xc3, -1} },
        { {0x44, -1}, {0xc4, -1} }, { {0x45, -1}, {0xc5, -1} }, { {0x46, -1}, {0xc6, -1} }, { {0x47, -1}, {0xc7, -1} },
        { {0x48, -1}, {0xc8, -1} }, { {0x49, -1}, {0xc9, -1} }, { {0x4a, -1}, {0xca, -1} }, { {0x4b, -1}, {0xcb, -1} },
        { {0x4c, -1}, {0xcc, -1} }, { {0x4d, -1}, {0xcd, -1} }, { {0x4e, -1}, {0xce, -1} }, { {0x4f, -1}, {0xcf, -1} },
        { {0x50, -1}, {0xd0, -1} }, { {0x51, -1}, {0xd1, -1} }, { {0x52, -1}, {0xd2, -1} }, { {0x56, -1}, {0xd6, -1} },
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*54*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*58*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*5c*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*60*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*64*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*68*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*6c*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*70*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*74*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*78*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*7c*/

        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*80*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*84*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*88*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*8c*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*90*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*94*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*98*/
        { {0x57, -1}, {0xd7, -1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*9c*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*a0*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*a4*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {0xaa, -1}, {0x2a, -1} },             { {-1},             {-1} },             /*a8*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*ac*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*b0*/
        { {-1},             {-1} },             { {0x35, -1}, {0xb5, -1} },             { {0xb6, -1}, {0x36, -1} },             { {0x37, -1}, {0xb7, -1} }, /*b4*/
        { {0x38, -1}, {0xb8, -1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*b8*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*bc*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*c0*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {0x46, -1}, {0xc6, -1} },             { {0x47, -1}, {0xc7, -1} }, /*c4*/
        { {0x29, -1}, {0xa9, -1} },             { {0x49, -1}, {0xc9, -1} },             { {-1},             {-1} },             { {0x2b, -1}, {0xab, -1} }, /*c8*/
        { {-1},             {-1} },             { {0x4e, -1}, {0xce, -1} },             { {-1},             {-1} },             { {0x4f, -1}, {0xcf, -1} }, /*cc*/
        { {0x4a, -1}, {0xca, -1} },             { {0x51, -1}, {0xd1, -1} },             { {0x52, -1}, {0xd2, -1} },             { {0x53, -1}, {0xd3, -1} }, /*d0*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*d4*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*d8*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*dc*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*e0*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*e4*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*e8*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*ec*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*f0*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*f4*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*f8*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*fc*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*100*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*104*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*108*/
        { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             { {-1},             {-1} },             /*10c*/
};
static int oldkey[272];
static int keydelay[272];

/* This array acts an intermediary so scan codes are processed in the correct order (ALT-CTRL-SHIFT-RSHIFT first, then all others). */
static int scorder[272] = {0x38, 0xB8, 0x1D, 0x9D, 0xFF, 0x2A, 0x36,0x103, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
			   0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
			   0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
			   0x29, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x39, 0x3A, 0x3B,
			   0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B,
			   0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B,
			   0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B,
			   0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B,
			   0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B,
			   0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B,
			   0x9C, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC,
			   0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD,
			   0xBE, 0xBF, 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD,
			   0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD,
			   0xDE, 0xDF, 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED,
			   0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD,
			   0xFE,0x100,0x101,0x102,0x104,0x105,0x106,0x107,0x108,0x109,0x10A,0x10B,0x10C,0x10D,0x10E,0x10F};

/* bit 0 = repeat, bit 1 = makes break code? */
int set3_flags[272];
uint8_t set3_all_repeat = 0;
uint8_t set3_all_break = 0;

void (*keyboard_send)(uint8_t val);
void (*keyboard_poll)();
int keyboard_scan = 1;

void keyboard_process()
{
        int c;
        int d;
        scancode *scancodes;

	if (AT)
	{
		switch (mode & 3)
		{
			case 1:
			default:
				scancodes = scancode_set1;
				break;
			case 2:
				scancodes = scancode_set2;
				break;
			case 3:
				scancodes = scancode_set3;
				break;
		}
		if (mode & 0x20)  scancodes = scancode_set1;
	}
	else
	{
		scancodes = scancode_xt;
	}

        if (!keyboard_scan) return;
        if (TANDY) scancodes = scancode_tandy;
                
        for (c = 0; c < 272; c++)
        {
                if (pcem_key[scorder[c]])
                        keydelay[scorder[c]]++;
                else
                        keydelay[scorder[c]] = 0;
        }

	for (c = 0; c < 272; c++)
        {
                if (pcem_key[scorder[c]] != oldkey[scorder[c]])
                {
                        oldkey[scorder[c]] = pcem_key[scorder[c]];
                        if ( pcem_key[scorder[c]] && scancodes[scorder[c]].scancodes_make[0]  == -1)
                           continue;
                        if (!pcem_key[scorder[c]] && scancodes[scorder[c]].scancodes_break[0] == -1)
                           continue;
			if (AT && ((mode & 3) == 3))
			{
				if (!set3_all_break && !pcem_key[scorder[c]] && !(set3_flags[scancodes[scorder[c]].scancodes_make[0]] & 2))
					continue;
			}
//                        pclog("Key %02X start\n", scorder[c]);
                        d = 0;
                        if (pcem_key[scorder[c]])
                        {
                                while (scancodes[scorder[c]].scancodes_make[d] != -1)
                                      keyboard_send(scancodes[scorder[c]].scancodes_make[d++]);
                        }
                        else
                        {
                                while (scancodes[scorder[c]].scancodes_break[d] != -1)
					keyboard_send(scancodes[scorder[c]].scancodes_break[d++]);
                        }
                }
        }

        for (c = 0; c < 272; c++)
        {
		if (AT && ((mode & 3) == 3))
		{
			if (scancodes[scorder[c]].scancodes_make[0] == -1)
				continue;
			if (!set3_all_repeat && !pcem_key[scorder[c]] && !(set3_flags[scancodes[scorder[c]].scancodes_make[0]] & 1))
				continue;
		}
                if (keydelay[scorder[c]] >= 30)
                {
                        keydelay[scorder[c]] -= 10;
                        if (scancodes[scorder[c]].scancodes_make[0] == -1)
                           continue;
                           
                        d = 0;

                        while (scancodes[scorder[c]].scancodes_make[d] != -1)
                              keyboard_send(scancodes[scorder[c]].scancodes_make[d++]);
                }
        }
}
